import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.SystemColor;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.awt.Font;
import java.awt.Color;

public class Menu {

	private JPanel MenuPanel;

	public Menu() {

		MenuPanel = new JPanel();
		MenuPanel.setLayout(new GridLayout(5, 1));

		//MyTicket, it calls MyTickets
		JButton btnMyTicket = new JButton("My Ticket");
		btnMyTicket.setFont(new Font("MV Boli", Font.BOLD, 17));
		btnMyTicket.setBackground(SystemColor.inactiveCaption);
		btnMyTicket.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				MyTickets mt;
				try {
					// read the currentID.txt
					FileReader currentIDReader = new FileReader("currentID.txt");
					BufferedReader currentMatchBufferedReader = new BufferedReader(
							currentIDReader);

					String line = null;
					if ((line = currentMatchBufferedReader.readLine()) != null) {
						try {
							mt = new MyTickets();
							mt.setBounds(0, 0, 500, 400);
							mt.setVisible(true);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					// error if not login.
					else {
						JOptionPane.showMessageDialog(MenuPanel, "LOGIN FIRST",
								"Error!", JOptionPane.ERROR_MESSAGE);
					}
				} catch (IOException e1) {

				}

			}
		});
		MenuPanel.add(btnMyTicket);

		//Cancel, it calls CancelReserve
		JButton btnCancelTicket = new JButton("Cancel Ticket");
		btnCancelTicket.setFont(new Font("MV Boli", Font.BOLD, 17));
		btnCancelTicket.setBackground(SystemColor.inactiveCaption);
		btnCancelTicket.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CancelReserve cr;
				try {
					FileReader currentIDReader = new FileReader("currentID.txt");
					BufferedReader currentMatchBufferedReader = new BufferedReader(
							currentIDReader);

					String line = null;
					if ((line = currentMatchBufferedReader.readLine()) != null) {

						try {
							cr = new CancelReserve();
							cr.setBounds(0, 0, 500, 330);
							cr.setVisible(true);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					} else {
						JOptionPane.showMessageDialog(MenuPanel, "LOGIN FIRST",
								"Error!", JOptionPane.ERROR_MESSAGE);
					}
				} catch (IOException e1) {

				}
			}
		});
		MenuPanel.add(btnCancelTicket);

		//MoreInfomation it calls FullSeat
		JButton btnMoreInformation = new JButton("Stadium Overview");
		btnMoreInformation.setFont(new Font("MV Boli", Font.BOLD, 17));
		btnMoreInformation.setBackground(SystemColor.inactiveCaption);
		btnMoreInformation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				FullSeat fs;
				try {
					FileReader currentIDReader = new FileReader("currentID.txt");
					BufferedReader currentMatchBufferedReader = new BufferedReader(
							currentIDReader);

					String line = null;
					if ((line = currentMatchBufferedReader.readLine()) != null) {

						fs = new FullSeat();
						fs.setBounds(0, 0, 900, 750);
						fs.setVisible(true);

					} else {
						JOptionPane.showMessageDialog(MenuPanel, "LOGIN FIRST",
								"Error!", JOptionPane.ERROR_MESSAGE);
					}
				} catch (IOException e1) {

				}
			}
		});
		MenuPanel.add(btnMoreInformation);
		
		//For the exit btn.
		final String[] confirm = { "Confirm", "No" };
		JButton btnExit = new JButton("EXIT");
		btnExit.setForeground(new Color(255, 0, 0));
		btnExit.setBackground(SystemColor.inactiveCaption);
		btnExit.setFont(new Font("MV Boli", Font.BOLD, 17));
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				int i = JOptionPane
						.showOptionDialog(
								null,
								"Thanks to use our System! Your account is now Logout!",
								"--BYE--", JOptionPane.OK_CANCEL_OPTION,
								JOptionPane.INFORMATION_MESSAGE, null, confirm,
								arg0);
				//i==0 Confirm;  i==1 No;
				if (i == 0) {

					FileWriter currentIDWriter;
					try {
						currentIDWriter = new FileWriter("currentID.txt");
						currentIDWriter.write(" ");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					System.exit(0);
				}
			}
		});
		MenuPanel.add(btnExit);
	}

	public JPanel menu() {
		return MenuPanel;

	}
}
